package com.ry.est.annotations;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;

@Slf4j
@Component
public class TemporalIntervalTimeSegmentValidator
        implements ConstraintValidator<TemporalIntervalTimeSegment,Integer> {

    int[] values;

    @Override
    public void initialize(TemporalIntervalTimeSegment constraintAnnotation) {
        this.values = constraintAnnotation.values();
        log.info(Arrays.toString(values));
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {

        if (value == null){
            return false;
        }

        int intValue = value.intValue();
        for (Integer val : values) {
            //使用基本类型来比较
            if (val == intValue){
                return true;
            }
        }


        return false;
    }
}
